

    
for kl = 1:size(Lnexp,2)
    Lnexp_cumulative(kl) = sum(Lnexp(1:kl)); % using (1-Wnr) has floating point issues, so use this instead
    ln_cumulative(kl) = sum(ln(1:kl)); % using (1-Wnr) has floating point issues, so use this instead
end
Lnexp_cumulative(Lnexp==0)=0;
ln_cumulative(ln==0)=0;

    
% FirstMeltPb = Pb(min(find(ln >0)));
FirstMeltPb_Index = (min(find(ln >0)));
lastmeltpressure = Pb(max(find(ln >0))); 
lastmeltpressure_Index = (max(find(ln >0))); 

FirstAt2percent = Pb(find(Mr >0,1));
FirstAt2percent_Index = (find(Mr >0,1));
FirstAt2percent_Index = (find(Lnexp >0,1));



    
if FirstAt2percent >= Gar2SpTransition
    firstgarnetmeltCL_OUT = (CL_OUT(FirstAt2percent_Index,:));
    firstgarnetmelt = (BAI4(FirstAt2percent_Index,:));
    importantpressure_gar1 = FirstAt2percent; 
    firstGar_FN = Lnexp_cumulative(FirstAt2percent_Index); 
    firstGar_DISEQ = temp_NF_DISEQ(FirstAt2percent_Index,:);
    
    firstspinelmeltCL_OUT = (CL_OUT(Gar2SpTransition_index+1,:)); 
    firstspinelmelt = (BAI4(Gar2SpTransition_index+1,:)); 
    firstSp_FN = Lnexp_cumulative(Gar2SpTransition_index+1); 
    firstSp_DISEQ = temp_NF_DISEQ(Gar2SpTransition_index+1,:); 
else
    firstgarnetmeltCL_OUT = NaN.*zeros(1,45);
    firstgarnetmelt = NaN.*zeros(1,9);
    importantpressure_gar1 = NaN; 
    firstGar_FN = NaN; 
    firstGar_DISEQ = NaN.*zeros(1,4);

    firstspinelmeltCL_OUT = (CL_OUT(FirstAt2percent_Index,:)); %will be an error if the first melt is in the plag field! (FIXED!)
    firstspinelmelt = (BAI4(FirstAt2percent_Index,:));
    firstSp_FN = Lnexp_cumulative(FirstAt2percent_Index);
    firstSp_DISEQ = temp_NF_DISEQ(FirstAt2percent_Index,:); 
    
    if isempty(firstspinelmeltCL_OUT)==1
    firstspinelmeltCL_OUT = NaN.*zeros(1,45);
    firstspinelmelt = NaN.*zeros(1,9);
    firstSp_FN = NaN;
    firstSp_DISEQ = NaN.*zeros(1,4);
    end
    
 
end

if Pb(FirstAt2percent_Index) < (Sp2PlagTransition)
    firstspinelmelt=NaN.*firstspinelmelt; 
    firstspinelmeltCL_OUT=NaN.*firstspinelmeltCL_OUT; 
    firstSp_FN = NaN;
  
end



if lastmeltpressure < Sp2PlagTransition
    lastplagmeltCL_OUT = (CL_OUT(lastmeltpressure_Index,:));
    lastplagmelt = (BAI4(lastmeltpressure_Index,:));
    importantpressure_sp3 = FirstAt2percent;
else
    lastplagmeltCL_OUT = NaN.*zeros(1,45);
    lastplagmelt = NaN.*zeros(1,9);
end


    if isempty(lastplagmeltCL_OUT)==1
    lastplagmeltCL_OUT = NaN.*zeros(1,45);
    lastplagmelt = NaN.*zeros(1,9);
    end

% XX CHECK THIS, IT WILL BE WRONG IF GARNET IS EXHAUSTED 

       


%               
%         for i = 1:45
%             poolCL_OUT_garnet(i) = sum(sum(Mr(1:Gar2SpTransition_index,:).*CL_OUT(1:Gar2SpTransition_index,i)))/sum(sum(Mr(1:Gar2SpTransition_index,:)));
%         end
%         
%         clear pool_garnet
%         for i = 1:9
%             pool_garnet(i) = sum(sum(Mr(1:Gar2SpTransition_index,:).*BAI4(1:Gar2SpTransition_index,i)))/sum(sum(Mr(1:Gar2SpTransition_index,:)));
%         end
%        
%         
        
       
       for i = 1:45
            poolCL_OUT_spinel(i) = sum(sum(Mr(Gar2SpTransition_index+1:Sp2PlagTransition_index,:).*CL_OUT(Gar2SpTransition_index+1:Sp2PlagTransition_index,i)))/sum(sum(Mr(Gar2SpTransition_index+1:Sp2PlagTransition_index,:)));
       end
       
       for i = 1:9
            pool_spinel(i) = sum(sum(Mr(Gar2SpTransition_index+1:Sp2PlagTransition_index,:).*BAI4(Gar2SpTransition_index+1:Sp2PlagTransition_index,i)))/sum(sum(Mr(Gar2SpTransition_index+1:Sp2PlagTransition_index,:)));
       end
        
       
        for i = 1:45
            poolCL_OUT_plag(i) = sum(sum(Mr(Sp2PlagTransition_index+1:end,:).*CL_OUT(Sp2PlagTransition_index+1:end,i)))/sum(sum(Mr(Sp2PlagTransition_index+1:end,:)));
       end
       
        for i = 1:9
            pool_plag(i) = sum(sum(Mr(Sp2PlagTransition_index+1:end,:).*BAI4(Sp2PlagTransition_index+1:end,i)))/sum(sum(Mr(Sp2PlagTransition_index+1:end,:)));
        end
       

        pool_gar_majors_temp = nan.*pool_plag; 
        pool_gar_trace_temp = nan.*poolCL_OUT_plag; 

          importanttracelements = [...
          firstgarnetmeltCL_OUT; 
          pool_gar_trace_temp  ;    
          (CL_OUT(Gar2SpTransition_index,:));            
          firstspinelmeltCL_OUT;
          poolCL_OUT_spinel;  
          (CL_OUT(Sp2PlagTransition_index,:));  
          (CL_OUT(Sp2PlagTransition_index+1,:)); 
          poolCL_OUT_plag ;
          lastplagmeltCL_OUT;
          zeros((size(poolCL_OUT_plag)));
          poolCL_Iv];     

          importantmajorelements = [...
          firstgarnetmelt; 
          pool_gar_majors_temp  ;    
          (BAI4(Gar2SpTransition_index,:));            
          firstspinelmelt;
          pool_spinel;  
          (BAI4(Sp2PlagTransition_index,:));  
          (BAI4(Sp2PlagTransition_index+1,:)); 
          pool_plag ;
          lastplagmelt;
          zeros((size(pool_plag)));
          poolBAI4_Iv];  
      
          importantFN = [...
          firstGar_FN
          NaN
          Lnexp_cumulative(Gar2SpTransition_index)
          firstSp_FN
          nan
          Lnexp_cumulative(Sp2PlagTransition_index)
          Lnexp_cumulative(Sp2PlagTransition_index+1)
          NaN
          Lnexp_cumulative(lastmeltpressure_Index)
          FP_Full
          FP_OnAxis];
          
      
          importantDISEQ = [...
          firstGar_DISEQ
           NaN.*zeros(1,4);
          temp_NF_DISEQ(Gar2SpTransition_index,:)
          firstSp_DISEQ
           NaN.*zeros(1,4);
          temp_NF_DISEQ(Sp2PlagTransition_index,:)
          temp_NF_DISEQ(Sp2PlagTransition_index+1,:)
           NaN.*zeros(1,4);
          temp_NF_DISEQ(lastmeltpressure_Index,:)
          FullPool_DisEQ
          NarrowPool_DisEQ];
          
      
      
      if Sp2PlagTransition>FirstAt2percent
      importantmajorelements(7,:) = (BAI4(FirstAt2percent_Index,:)); 
      importanttracelements(7,:) = (CL_OUT(FirstAt2percent_Index,:));  
      %importantFN(7)= Lnexp_cumulative(FirstAt2percent_Index); 
      end
      
% %         importantPressures= [...
% %           FirstAt2percent; 
% %           pool_garnet  ;    
% %           squeeze(BAI4(101,Gar2SpTransition_index,:))';            
% %           squeeze(BAI4(101,Gar2SpTransition_index+1,:))';  
% %           pool_spinel;  
% %           squeeze(BAI4(101,Sp2PlagTransition_index,:))';  
% %           squeeze(BAI4(101,Sp2PlagTransition_index+1,:))'; 
% %           pool_plag ;
% %           lastplagmelt
% %           pool
% %           pool_ininc
          
          
          
      
        importantCompositions  = [importantmajorelements importanttracelements]; 
        
        
        importantCompositions(2,:) = pool_garnet_SAVE; 
        importantCompositions(10,:) = [pool_full poolCL_full]; 
        
        
        %for isobaric melting
        if size(importantCompositions,1)==10
            importantCompositions(11,:) = nan.*importantCompositions(10,:) ;
            importantFN([10:11],:) = nan.*importantFN([1:2],:) ;
        end
   return     
        %mat2CL_OUTip(importantCompositions)
    
      
          
%         OnAxisInstantMelts = [squeeze(BAI4(101,:,:)) squeeze(CL_OUT(101,:,:))]; 
%         mat2CL_OUTip(OnAxisInstantMelts)
%         
%         OffAxisInstantMelts = [squeeze(BAI4(IncrementalWingsIndex(1),:,:)) squeeze(CL_OUT(IncrementalWingsIndex(1),:,:))]; 
%         mat2CL_OUTip(OffAxisInstantMelts)
%%
    num=674; 
    extrameltCL_OUT = (CL_OUT(num,:)); 
    extraMelt = (BAI4(num,:)); 
    mat2clip([extraMelt extrameltCL_OUT])
     %%     


%        
%              for k = 0:1:length(x)/2
%             for i = 1:45
%                 poolCL_OUT_ininc(k+1,i) = sum(sum(Mr(:,1+k:end-k).* ...
%                 CL_OUT(1+k:end-k,:,i)'))/sum(sum(Mr(:,1+k:end-k)));
%             end
%              end
            
            
            



%         pool_ininc(:,10) = ((pool_ininc(:,6)/40.311))./(((pool_ininc(:,6)./40.3011)+((pool_ininc(:,5)./71.864))));
%         pool(:,10) =  ((pool(:,6)/40.311)./(((pool(:,6)./40.311)+((pool(:,5)./71.864))));
        
        
%if you wanted to figure out the pooled compostion of the wings:        
% %             for i = 1:size(XO,3)
% %             poolXO_wings(i,:) = sum(sum(Mr.*CL_OUT(:,:,i)'))/sum(Mr(:));
% %             end
% %             
% %                     for i = 1:9
% %             pool(i) = sum(sum(Mr.*BAI4(:,:,i)'))/sum(Mr(:));
% %                     end
% %                     
% %                     
% %                     
% %             for k = 0:1:length(x)/2
% %             for i = 1:45
% %                 poolCL_OUT_ininc(k+1,i) = sum(sum(Mr(:,1+k:end-k).* ...
% %                 CL_OUT(1+k:end-k,:,i)'))/sum(sum(Mr(:,1+k:end-k)));
% %             end
% %             end
                    
        
                    